<?php
namespace app\admin\controller;

use app\admin\model\EarlyWaring as EarlyWaringModel;
use think\Db;
use think\Request;

class CarEvent extends Base
{
    public function index()
    {
        $model = new EarlyWaringModel;

        //渲染页面模板
        $model->field('e.id,e.detail,e.position,e.created_at,e.updated_at, u.username, d.name as dname, d.imei, t.name as tname,c.plate_number')
            ->alias('e')
            ->join('user u','e.user_id = u.id ','left')
            ->join('device d','e.device_id = d.id','left')
            ->join('earlywarning_type t','e.earlywarning_type_id = t.id','left')
            ->join('car c','e.car_id = c.id','left')
            ->order('e.id','desc');
            //->select();
           // ->paginate(5);

        //搜索名称
        $phname = $this->request->param("phname");

        if(!empty($phname)){
           $model->where('d.name|d.imei','like', '%'.$phname.'%');
        }
        $result = $model->paginate(10,false,[
            'query'=>['phname'=>$phname]
        ]);

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];
            $view = $model->getAll()->where("id",$id)->select();
            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }

        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('carevent',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }
}